﻿using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VIP.Extension;

public class TrialTool {

    /// <summary>
    /// 读取信息
    /// </summary>
    /// <returns></returns>
    public static void ReadInfo() {
        string encryptStr = VIPFileUtil.ReadAllText("trial.dll",VIPPathEnum.STREAMING_ASSETS_PATH);
        string decryptStr = VIPEncryptUtil.DecryptDES(encryptStr);
        TrialEntity te = JsonMapper.ToObject<TrialEntity>(decryptStr);
        TrialEntity.Instance.StartDate = te.StartDate;
        TrialEntity.Instance.Days = te.Days;
        TrialEntity.Instance.EndDate = te.EndDate;
        TrialEntity.Instance.IsActive = te.IsActive;
        TrialEntity.Instance.MachineCode = te.MachineCode;
        TrialEntity.Instance.RegisterCode = te.RegisterCode;
    }

    /// <summary>
    /// 试用剩余时间
    /// </summary>
    public static int RemainTime() {
        //获取系统当前时间
        string currDate = VIPConvertUtil.DateTimeToString(DateTime.Now);
        int days = VIPComputeUtil.ComputeDifferDay(currDate, TrialEntity.Instance.EndDate);
        return days;
    }

    /// <summary>
    /// 保存信息
    /// </summary>
    /// <param name="str"></param>
    public static void WriteInfo(TrialEntity entity) {
        string str = JsonMapper.ToJson(entity);
        string encryptStr = str.Unescape().DES();
        VIPFileUtil.WriteAllText("trial.dll", encryptStr,VIPPathEnum.STREAMING_ASSETS_PATH);
    }

    /// <summary>
    /// 检查试用是否结束
    /// </summary>
    public static bool CheckTrialIsEnd() {
        //获取系统当前时间
        string currDate = VIPConvertUtil.DateTimeToString(DateTime.Now);
        //软件试用的总天数-(软件试用结束时间-当前时间得出的天数)
        int days = TrialEntity.Instance.Days - VIPComputeUtil.ComputeDifferDay(currDate, TrialEntity.Instance.EndDate);
        if (days < TrialEntity.Instance.Days)
        {
            //试用期还没到
            return false;
        }
        else {
            //试用期结束
            return true;
        }
    }

    public static string GetCode()
    {
        string content = VIPEncryptUtil.GetMachineCode().MD5();
        return content;
    }

    /// <summary>
    /// 获取二维码
    /// </summary>
    public static Sprite GetCode(Texture2D texture) {
        string content = VIPEncryptUtil.GetMachineCode().MD5();
        Texture2D t2d = VIPQRUtil.GenerateQRCode("机器码为：" + content, 400, 400, texture);
        return VIPConvertUtil.Texture2DToSprite(t2d);
    }

    /// <summary>
    /// 检查注册码是否正确
    /// </summary>
    /// <param name="registerCode"></param>
    /// <returns></returns>
    public static bool CheckRegisterCode(string registerCode) {
        if (registerCode.Equals(TrialEntity.Instance.RegisterCode)) {
            return true;
        }
        return false;
    }

}
